package com.jeesite.modules.app.course.question;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.course.answers.entity.TcourseAnswers;
import com.jeesite.modules.zx.course.answers.service.TcourseAnswersService;
import com.jeesite.modules.zx.course.question.entity.TcourseQuestion;
import com.jeesite.modules.zx.course.question.service.TcourseQuestionService;

@Controller
@RequestMapping(value = "/app/course/question/")
public class AppCourseQuestionController {
	@Autowired
	private TcourseQuestionService tcourseQuestionService;
	@Autowired
	private TcourseAnswersService tcourseAnswersService;
	@RequestMapping(value = "findPage")
	public void findPage(TcourseQuestion entity, HttpServletRequest request,HttpServletResponse response){
		entity.setPage(new Page<>(request, response));
		Page<TcourseQuestion> findPage = tcourseQuestionService.findPage(entity);
		TcourseAnswers answer=null;
		for(TcourseQuestion cqu:findPage.getList()){
			answer=new TcourseAnswers();
			String id = cqu.getId();
			answer.setQuestionId(id);
			List<TcourseAnswers> findList = tcourseAnswersService.findList(answer);
			cqu.setAnswers(findList);
		}
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value = "save")
	public void save(TcourseQuestion entity, HttpServletRequest request,HttpServletResponse response){
		String courseId = entity.getCourseId();
		if(StringUtils.isBlank(courseId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "课程标识");
			return;
		}
		String askUserId = entity.getAskUserId();
		if(StringUtils.isBlank(askUserId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		String userName = entity.getUserName();
		if(StringUtils.isBlank(userName)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户名称");
			return;
		}
		String content=entity.getContent();
		if(StringUtils.isBlank(content)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "提问内容");
			return;
		}
		tcourseQuestionService.save(entity);
		AppController.renderSuccessObject(response, "", MessageCode.SUCCESS_ACK);
	}
}
